﻿layui.config({
    base: '/Lib/layui/extend/'
}).extend({
    formSelects: 'formSelects-v4'
}).use(['form', 'layer', 'table', 'laypage', 'laydate', 'jquery'], function () {
    var form = layui.form,
        $ = layui.jquery,
        table = layui.table,
        player = parent.layer || layui.layer,
        laydate = layui.laydate;

    var clockOnListLogic = {
        //初始化
        init: function () {

            self = this;

            this.Parameters = {
                ac: new AsyncCaller(),    //异步方法管理
                title: [],
                field: 'Name',
                order: 'asc',
                projectAllList: "/Project/Info/LoadProjectAllList",
                listUrl: "/Staff/ClockOn/LoadRecordList",
                exportInfoUrl: "/Staff/ClockOn/EpxortClockOn"
            };

            this.Parameters.ac
                .pushQueue(function () {
                    CommonLogic.loadButtonsInfo(function () {
                        self.Parameters.ac.notifyQueueAsyncFuncComplate();
                    })
                })
                .pushQueue(function () {
                    self.logicFunc.loadProjectAllList(function () {
                        self.Parameters.ac.notifyQueueAsyncFuncComplate();
                    });
                })
                .pushQueue(function () {
                    self.initPage();
                    self.Parameters.ac.notifyQueueAsyncFuncComplate();
                })
                .pushQueue(this.bindEvent)
                .exec();

            //this.initPage();
            //this.bindEvent();
        },
        //初始化列表
        initPage: function () {

            //日期
            var lastMoth = new Date(new Date().setMonth(new Date().getMonth() - 1));
            var startTime = laydate.render({
                elem: '#txtBeginTime',
                format: 'yyyy-MM-dd',
                value: lastMoth,
                done: function (value, date, endDate) {
                    endLayDate.config.min = {
                        year: date.year,
                        month: date.month - 1,
                        date: date.date
                        /* hours: date.hours,
                           minutes: date.minutes,
                           seconds: date.seconds +1*/
                    };
                }
            });

            var endLayDate = laydate.render({
                elem: '#txtEndTime',
                format: 'yyyy-MM-dd',
                value: new Date(),
                done: function (value, date, endDate) {
                    startTime.config.max = {
                        year: date.year != undefined ? date.year : 2099,
                        month: (date.month != undefined) ? (date.month - 1) : 12,
                        date: (date.date != undefined) ? date.date : 25
                        /* hours: date.hours,
                           minutes: date.minutes,
                           seconds: date.seconds -1*/
                    };
                }
            });

            //获取用户信息
            var title =
                [
                    { type: 'checkbox', fixed: 'left' },
                    { field: 'ID', title: 'ID', hide: true },
                    { field: 'UserNo', title: '编号', width: 180, align: "center", sort: true, fixed: 'left' },
                    { field: 'Name', title: '姓名', width: 130, align: "center", sort: true, fixed: 'left' },
                    {
                        field: 'Sex', title: '性别', width: 80, align: "center", fixed: 'left',
                        templet: function (d) {
                            return GrdEnum.getValue.userSexValue(d.Sex.trim());
                        }
                    },
                    { field: 'ProjectName', title: '项目名称', width: 120, align: "center", fixed: 'left' },
                    {
                        field: 'AttendType', title: '类型', width: 120, align: "center",
                        templet: function (d) {
                            if (d.AttendType==1) {
                                return "上班";
                            } else {
                                return "下班";
                            }
                        }
                    },
                    {
                        field: 'AttTime', title: '时间', width: 160, align: "center",
                        templet: function (d) {
                            return GrdAssist.formatDate(d.AttTime, "yyyy-MM-dd HH:mm:ss");
                        }
                    },
                    { field: 'Lon', title: '经度', width: 150, align: "center" },
                    { field: 'Lat', title: '纬度', width: 150, align: "center" },
                    { field: 'AttAddress', title: '地点', width: 350, align: "left" }
                ];

            //table加载
            GrdLayUI.table({
                dom: '#InfoList',
                //height: "full-120",
                url: self.Parameters.listUrl,
                field: self.Parameters.field,
                order: self.Parameters.order,
                parameters: {
                    ProjectNo: $("#ProjectNo").val(),
                    KeyWord: $("#KeyWord").val(),
                    //PYM: $("#PYM").val(),
                    dBeginTime: $("#txtBeginTime").val(),
                    dEndTime: $("#txtEndTime").val()
                },
                title: title
            });

            //排序监听
            GrdLayUI.tableSort({
                domId: "InfoList",
                backAction: function (obj) {
                    self.Parameters.field = obj.field;
                    self.Parameters.order = obj.type;
                    self.logicFunc.loadListInfo();
                }
            });
            form.render();
        },
        //绑定事件
        bindEvent: function () {

            //查询
            $("#funBtnSearch").on("click", function () {
                self.logicFunc.loadListInfo();
            });

            //导出
            $("#funBtnExport").on("click", function () {
                self.logicFunc.exportInfo();
            });
        },
        /*
         * 逻辑方法
         */
        logicFunc: {
            //获取所有的工程信息
            loadProjectAllList: function (func) {
                GrdLayUI.ajaxPost({
                    url: self.Parameters.projectAllList,
                    data: "",
                    success: function (result) {
                        if (result.Code == 1000) {
                            $("#ProjectNo").empty();
                            $("#ProjectNo").append("<option value=''>所有工程项目</option>");
                            for (var i = 0; i < result.Data.length; i++) {
                                $("#ProjectNo").append("<option value=" + result.Data[i].ProjectNo + ">" + result.Data[i].ProjectName + "</option>");
                            }
                        }
                        if (func) {
                            func();
                        }
                    }
                });
            },

            ///获取数据
            loadListInfo: function () {

                GrdLayUI.tableReload({
                    domId: "InfoList",
                    field: self.Parameters.field,
                    order: self.Parameters.order,
                    parameters: {
                        ProjectNo: $("#ProjectNo").val(),
                        KeyWord: $("#KeyWord").val(),
                        //PYM: $("#PYM").val(),
                        dBeginTime: $("#txtBeginTime").val(),
                        dEndTime: $("#txtEndTime").val()
                    }
                });
            },
            //导出信息
            exportInfo: function () {
                var json = {
                    ProjectNo: $("#ProjectNo").val(),
                    Name: $("#Name").val(),
                    PYM: $("#PYM").val(),
                    dBeginTime: $("#txtBeginTime").val(),
                    dEndTime: $("#txtEndTime").val()
                };
                GrdLayUI.ajaxPost({
                    url: self.Parameters.exportInfoUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {
                            window.open("http://" + location.host + result.Msg, "_self");
                        }
                    }
                });
            },
        },
        //工具
        tools: {

        }
    };
    clockOnListLogic.init();
});
